<?php

class BadWordsController extends MainController implements ControllerInterface {

	//o - order
	//v - order vector
	//t - total items to select
	public $tableConfig = array(
		'bad_words' => array(
			'o' => array(
				'id' => 'bad_words.id_bad_words',
				'word' => 'bad_words.word',
				'active' => 'bad_words.active',
				'_' => 'word'
			),
			'v' => 'malejaco',
			't' => 10
		)
	);

	public function preDispatch($param) {
		$this->Run($param);
		$this->RunShared('Auth', array());
	}

	public function postDispatch($param) {
		
	}

	public function IndexAction($param) {
		try {
			$this->smarty->assign('table_type','bad_words');
			$dal = BadWordsDAL::GetDalDataObj(array(
						'tableData' => $this->tableParams['bad_words'],
						'condition' => array()
					));

			$list = BadWordsDAL::GetResult($dal);
			$dal->setCount(true);
			$dal->setLimit('');
			$count = BadWordsDAL::GetResult($dal);

			$this->smarty->assign('list', $list);
			$this->smarty->assign('c', $count);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
	}
	
	public function editAction($param) {
		if(Request::IsPost()) {
			$active = Request::GetPost('active');
			$active = empty($active) ? 0 : 1;
			$bw = new BadWords();
			$bw->setId($param['id']);
			$bw->setWord(Request::GetPost('word'));
			$bw->setActive($active);
			
			BadWordsDAL::Save($bw->GetDalDataObj());
			$this->addRedirectInfo('Zmiany zostały zapisane', 'ok', array('BadWords' => 'Index'));
		}
		
		$bw = BadWordsDAL::GetResult(BadWordsDAL::GetDalDataObj((array('limit' => 1,'condition' => array('id_bad_words' => $param['id'])))));
		$this->smarty->assign('wordObj', $bw[0]);
	}
	
	public function addAction($param) {
		$this->smarty->assign('addWord', 1);
		$this->partialTemplate = 'Edit.tpl';
		$post = Request::GetAllPost();
		
		if(Request::IsPost()) {
			$bw = new BadWords();
			$bw->setWord(Request::GetPost('word'));
			$bw->setActive(Request::GetPost('active'));
			
			BadWordsDAL::Save($bw->GetDalDataObj());
			$this->addRedirectInfo('Dodano zakazane słowo', 'ok', array('BadWords' => 'Index'));
		}
	}
	
	public function ToggleStatusAction($param) {
		$this->setNoRender();
		BadWordsDAL::DefaultToggleFieldById(BadWordsDAL::GetDalDataObj(array('id' => $param['id'])), 'active');
	}

}

?>
